import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';
import { Request, Response } from 'express';

@Catch()
export class ErrorExceptionFilter implements ExceptionFilter {
  catch(error: Error, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();

    let status = HttpStatus.INTERNAL_SERVER_ERROR;
    let message = 'Internal Server Error';
    console.log(error,'error');
    if (error instanceof HttpException) {
      status = error.getStatus();
      message = error.getResponse() as string;
    }

    response.status(status).json({
      statusCode: status,
      code: status,
      timestamp: new Date().toISOString(),
      path: request.url,
      message: message,
    });
  }
}